package lk.icta.core.web.action;

import lk.icta.common.web.action.BaseAction;
import lk.icta.core.service.AlienationMethodsManagementService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.apache.struts2.interceptor.SessionAware;

import lk.icta.core.entity.AlienationMethod;
import java.util.*;


/**
 * @author Sisiruwan Senadeera
 */

public class IntroduceNewAlienationMethodsAction extends BaseAction {

    private static final Logger logger = LoggerFactory.getLogger(IntroduceNewAlienationMethodsAction.class);

    private final AlienationMethodsManagementService alienationMethodsManagementService;
    private AlienationMethod alienationMethod;
    private List<AlienationMethod> alienationMethodList;

    private int editMode;
    private int alienationMethodId;


    public IntroduceNewAlienationMethodsAction(AlienationMethodsManagementService alienationMethodsManagementService) {
        this.alienationMethodsManagementService = alienationMethodsManagementService;
    }

    public String homePageLoad() {
        logger.debug("Load Introduce New Alienation Methods ");
        return SUCCESS;
    }

    public String createNewAlieanationPageLoad() {
        logger.debug("Create A New Alienation Method ");
        return SUCCESS;
    }

    public String createNewAlienationMethod() {
        if(validateUserInputData()){
            return ERROR;
        }

        try {

            alienationMethod.setState(AlienationMethod.State.DATA_ENTRY);
            alienationMethodsManagementService.addAlienationMethod(alienationMethod,user);
            addActionMessage("Alienation Method saved successfully");

            return SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ERROR;
        }
    }

    //Populate Basic Lists
    protected void populateBasicLists() {
        try{
            alienationMethodList = alienationMethodsManagementService.getAllUpdatableAlienationMethods();
        }
        catch(Exception e){
            logger.debug("Error: ", e.getMessage());
            e.printStackTrace();
        }
    }

    public String loadAlienationMethodsList() {
        logger.debug("Loading Updatable Alienation Methods List ");
        populateBasicLists();
        return SUCCESS;
    }

    // load AlienationMethod Form for editing

    public String loadAlienationMethodForUpdate(){
        editMode=1;
        try{
            alienationMethod=alienationMethodsManagementService.getAlienationMethodById(alienationMethodId,user);
            return SUCCESS;
        }
        catch(Exception e){
            e.printStackTrace();
            return ERROR;
        }
    }

    public String updateAlienationMethod(){
        if(validateUserInputData()){
            editMode=1;
            return ERROR;
        }

        try{
           alienationMethod.setId(alienationMethodId);
           alienationMethod.setState(AlienationMethod.State.DATA_ENTRY);
           alienationMethodsManagementService.updateAlienationMethod(alienationMethod,user);
           addActionMessage("Alienation Method updated successfully");
           return SUCCESS;
        }
        catch(Exception e){
            e.printStackTrace();
            return ERROR;
        }
    }

    private boolean validateUserInputData(){
        boolean bstatus=false;
        if (alienationMethod.getEnName()== null || alienationMethod.getEnName().isEmpty()) {
              bstatus=true;
              addActionError(getText("pls_Enter_Alienation_Method_Name_inen.label"));
        } else if (alienationMethod.getSiName()== null || alienationMethod.getSiName().isEmpty()) {
              bstatus=true;
              addActionError(getText("pls_enter_elienation_method_name_insi.label"));
        } else if (alienationMethod.getTaName()== null || alienationMethod.getTaName().isEmpty()) {
              bstatus=true;
              addActionError(getText("pls_enter_elienation_method_name_inta.label"));
        }
        return bstatus;
    }

    public void setAlienationMethod(AlienationMethod alienationMethod) {
        this.alienationMethod = alienationMethod;
    }

    public AlienationMethod getAlienationMethod() {
        return alienationMethod;
    }

    public List<AlienationMethod> getAlienationMethodList() {
        return alienationMethodList;
    }

    public void setAlienationMethodList(List<AlienationMethod> alienationMethodList) {
        this.alienationMethodList = alienationMethodList;
    }

    public int getEditMode() {
        return editMode;
    }

    public void setEditMode(int editMode) {
        this.editMode = editMode;
    }

    public int getAlienationMethodId() {
        return alienationMethodId;
    }

    public void setAlienationMethodId(int alienationMethodId) {
        this.alienationMethodId = alienationMethodId;
    }
}
